package com.myweb;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class WebRequest {

  private HttpServletRequest httpRequest;

  public WebRequest(HttpServletRequest httpRequest) {
    this.httpRequest = httpRequest;
  }

  public String getString(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      throw new WebException(key + " expected");
    }
    return param;
  }

  public String getString(String key, int max) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param == null) {
      throw new WebException(key + " expected");
    }
    if (param.length() > max) {
      throw new WebException(key + " has reached " + max + " chars");
    }
    return param;
  }

  public Integer getInteger(String key) throws WebException {
    String param = getString(key);
    if (param.length() > 0) {
      try {
        return Integer.parseInt(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be an integer");
      }
    } else {
      return null;
    }
  }

  public Long getLong(String key) throws WebException {
    String param = getString(key);
    if (param.length() > 0) {
      try {
        return Long.parseLong(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be long");
      }
    } else {
      return null;
    }
  }

  public Float getFloat(String key) throws WebException {
    String param = getString(key);
    if (param.length() > 0) {
      try {
        return Float.parseFloat(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be float");
      }
    } else {
      return null;
    }
  }

  public Boolean getBoolean(String key) throws WebException {
    String param = getString(key);
    if (param.length() > 0) {
      try {
        return Boolean.valueOf(param);
      } catch (NumberFormatException e) {
        throw new WebException(key + " should be boolean");
      }
    } else {
      return null;
    }
  }

  public Date getDate(String key, String format) throws WebException {
    DateFormat df = new SimpleDateFormat(format);
    String param = getString(key);
    if (param.length() > 0) {
      try {
        return df.parse(param);
      } catch (ParseException ex) {
        throw new WebException(key + " should be a date");
      }
    } else {
      return null;
    }
  }

  public String getStringNN(String key) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param != null && param.length() > 0) {
      return param;
    } else {
      throw new WebException(key + " expected");
    }
  }

  public String getStringNN(String key, int max) throws WebException {
    String param = httpRequest.getParameter(key);
    if (param != null && param.length() > 0) {
      if (param.length() > max) {
        throw new WebException(key + " has reached " + max + " chars");
      }
      return param;
    } else {
      throw new WebException(key + " expected");
    }
  }

  public Integer getIntegerNN(String key) throws WebException {
    String param = getStringNN(key);
    try {
      return Integer.parseInt(param);
    } catch (NumberFormatException e) {
      throw new WebException(key + " should be an integer");
    }
  }

  public Long getLongNN(String key) throws WebException {
    String param = getStringNN(key);
    try {
      return Long.parseLong(param);
    } catch (NumberFormatException e) {
      throw new WebException(key + " should be long");
    }
  }

  public Float getFloatNN(String key) throws WebException {
    String param = getStringNN(key);
    try {
      return Float.parseFloat(param);
    } catch (NumberFormatException e) {
      throw new WebException(key + " should be float");
    }
  }

  public Boolean getBooleanNN(String key) throws WebException {
    String param = getStringNN(key);
    try {
      return Boolean.valueOf(param);
    } catch (NumberFormatException e) {
      throw new WebException(key + " should be boolean");
    }
  }

  public Date getDateNN(String key, String format) throws WebException {
    DateFormat df = new SimpleDateFormat(format);
    String param = getStringNN(key);
    try {
      return df.parse(param);
    } catch (ParseException ex) {
      throw new WebException(key + " should be a date");
    }
  }

  public String[] getStrings(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    if (param == null) {
      throw new WebException(key + " expected");
    }
    return param;
  }

  public Integer[] getIntegers(String key) throws WebException {
    String[] param = getStrings(key);
    Integer[] list = new Integer[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Integer.parseInt(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        list[i] = null;
      }
    }
    return list;
  }

  public Long[] getLongs(String key) throws WebException {
    String[] param = getStrings(key);
    Long[] list = new Long[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Long.parseLong(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        list[i] = null;
      }
    }
    return list;
  }

  public Float[] getFloats(String key) throws WebException {
    String[] param = getStrings(key);
    Float[] list = new Float[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Float.parseFloat(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        list[i] = null;
      }
    }
    return list;
  }

  public Boolean[] getBooleans(String key) throws WebException {
    String[] param = getStrings(key);
    Boolean[] list = new Boolean[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Boolean.parseBoolean(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        list[i] = null;
      }
    }
    return list;
  }

  public Date[] getDates(String key, String format) throws WebException {
    DateFormat df = new SimpleDateFormat(format);
    String[] param = getStrings(key);
    Date[] list = new Date[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = df.parse(param[i]);
        } catch (ParseException ex) {
          throw new WebException(key + " should be a date");
        }
      } else {
        list[i] = null;
      }
    }
    return list;
  }

  public String[] getStringsNN(String key) throws WebException {
    String[] param = httpRequest.getParameterValues(key);
    if (param == null) {
      throw new WebException(key + " expected");
    }
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() == 0) {
        throw new WebException(key + " expected");
      }
    }
    return param;
  }

  public Integer[] getIntegersNN(String key) throws WebException {
    String[] param = getStrings(key);
    Integer[] list = new Integer[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Integer.parseInt(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        throw new WebException(key + " expected");
      }
    }
    return list;
  }

  public Long[] getLongsNN(String key) throws WebException {
    String[] param = getStrings(key);
    Long[] list = new Long[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Long.parseLong(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        throw new WebException(key + " expected");
      }
    }
    return list;
  }

  public Float[] getFloatsNN(String key) throws WebException {
    String[] param = getStrings(key);
    Float[] list = new Float[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Float.parseFloat(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        throw new WebException(key + " expected");
      }
    }
    return list;
  }

  public Boolean[] getBooleansNN(String key) throws WebException {
    String[] param = getStrings(key);
    Boolean[] list = new Boolean[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = Boolean.parseBoolean(param[i]);
        } catch (NumberFormatException e) {
          throw new WebException(key + " should be an integer");
        }
      } else {
        throw new WebException(key + " expected");
      }
    }
    return list;
  }

  public Date[] getDatesNN(String key, String format) throws WebException {
    DateFormat df = new SimpleDateFormat(format);
    String[] param = getStrings(key);
    Date[] list = new Date[param.length];
    for (int i = 0; i < param.length; i++) {
      if (param[i].length() > 0) {
        try {
          list[i] = df.parse(param[i]);
        } catch (ParseException ex) {
          throw new WebException(key + " should be a date");
        }
      } else {
        throw new WebException(key + " expected");
      }
    }
    return list;
  }

  public HttpSession getSession() {
    return httpRequest.getSession();
  }

  public boolean has(String param) {
    return httpRequest.getParameter(param) != null;
  }
}
